package com.mall.controller;

import com.mall.domain.Address;
import com.mall.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AddressController {
	@Autowired
	private IAddressService iAddressService;

	@RequestMapping("/findAlladdress")
	public @ResponseBody
	Map<String,Object> findAllAddress(){
		Map<String,Object> categoriesMap = new HashMap<>();
		//获取商品类别集合
		List<Address> categories = iAddressService.findAllAddress();
		//放入Map集合
		categoriesMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","获取商品类别列表成功");
		metaMap.put("status",200);

		categoriesMap.put("meta",metaMap);
		return categoriesMap;
	}

	@RequestMapping("/findAddressById")
	public @ResponseBody
	Map<String,Object> findAddressById(HttpServletRequest request){
		Integer id = Integer.parseInt(request.getParameter("id"));
		Map<String,Object> categoriesMap = new HashMap<>();
		//获取商品类别集合
		List<Address> categories = iAddressService.findAddressById(id);
		//放入Map集合
		categoriesMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","获取商品类别列表成功");
		metaMap.put("status",200);

		categoriesMap.put("meta",metaMap);
		return categoriesMap;
	}

	@RequestMapping("/updateAddress")
	public @ResponseBody
	Map<String,Object> updateAddress(Address address){

		Map<String,Object> addressMap = new HashMap<>();
		//获取商品类别集合
		int newAddress = iAddressService.updateAddress(address);
		//放入Map集合
		addressMap.put("newAddress",newAddress);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","地址修改成功");
		metaMap.put("status",200);

		addressMap.put("meta",metaMap);
		return addressMap;
	}

	@RequestMapping("/deleteAddress")
	public @ResponseBody
	Map<String,Object> deleteAddress(HttpServletRequest request){
		Integer id = Integer.parseInt(request.getParameter("id"));
		Map<String,Object> addressMap = new HashMap<>();
		//获取商品类别集合
		int newAddress = iAddressService.deleteAddress(id);
		//放入Map集合
		addressMap.put("newAddress",newAddress);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","删除地址成功");
		metaMap.put("status",200);

		addressMap.put("meta",metaMap);
		return addressMap;
	}


	@RequestMapping("/addAddress")
	public @ResponseBody
	Map<String,Object> addAddress(Address address){

		Map<String,Object> map = new HashMap<>();
		Map<String,Object> addressMap = new HashMap<>();
		//获取商品类别集合
		int newAddress = iAddressService.addAddress(address);
		//放入Map集合
		addressMap.put("newAddress",newAddress);
		System.out.println(addressMap);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","添加地址成功");
		metaMap.put("status",200);

		map.put("newAddress",newAddress);
		map.put("meta",metaMap);

		return map;
	}

}
